<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>{{ isCreate ? '创建采集器信息' : '更新采集器信息' }}</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="采集器ID" prop="collector_id">
                <el-input size="medium" v-model="form.collector_id" placeholder="请填写采集器ID"></el-input>
              </el-form-item>
              <el-form-item label="采集器名称" prop="collector_name">
                <el-input size="medium" v-model="form.collector_name" placeholder="请填写采集器名称"></el-input>
              </el-form-item>
              <el-form-item label="部门名称" prop="dwbh">
                <el-select size="medium" filterable v-model="form.dwbh" placeholder="请选择">
                  <el-option v-for="item in data1" :key="item.indexid" :label="item.name" :value="item.indexid">
                  </el-option>
                </el-select>
                <!-- <el-input size="medium" v-model="form.dwbh" placeholder="请填写部门名称"></el-input> -->
              </el-form-item>
              <el-form-item label="网关名称" prop="bgid">
                <el-select size="medium" filterable v-model="form.bgid" placeholder="请选择">
                  <el-option v-for="item in data2" :key="item.indexid" :label="item.name" :value="item.indexid">
                  </el-option>
                </el-select>
                <!-- <el-input size="medium" v-model="form.bgid" placeholder="请填写网关名称"></el-input> -->
              </el-form-item>
              <el-form-item label="分组编号" prop="group_id">
                <el-input size="medium" v-model="form.group_id" placeholder="请填写分组编号"></el-input>
              </el-form-item>
              <el-form-item label="规格型号" prop="specifications_model">
                <el-input size="medium" v-model="form.specifications_model" placeholder="请填写规格型号"></el-input>
              </el-form-item>
              <el-form-item label="生产厂家" prop="manufacturers">
                <el-input size="medium" v-model="form.manufacturers" placeholder="请填写生产厂家"></el-input>
              </el-form-item>
              <el-form-item label="采集器类型" prop="collector_type">
                <el-input size="medium" v-model="form.collector_type" placeholder="请填写采集器类型"></el-input>
              </el-form-item>
              <el-form-item label="序列号" prop="collector_number">
                <el-input size="medium" v-model="form.collector_number" placeholder="请填写采集器序列号"></el-input>
              </el-form-item>
              <el-form-item label="状态" prop="collector_state">
                <el-input size="medium" v-model="form.collector_state" placeholder="请填写采集器状态"></el-input>
              </el-form-item>
              <el-form-item label="温度范围" prop="t">
                <el-input style="width: 40%" size="medium" v-model="form.thigh" placeholder="请填写"
                  ><template slot="append">℃（上限）</template></el-input
                ><span style="font-size: 50px; color: #777">-</span>
                <el-input style="width: 40%" size="medium" v-model="form.tlower" placeholder="请填写"
                  ><template slot="append">℃（下限）</template></el-input
                >
              </el-form-item>
              <el-form-item label="湿度范围" prop="h">
                <el-input style="width: 40%" size="medium" v-model="form.hhigh" placeholder="请填写"
                  ><template slot="append">RH%（上限）</template></el-input
                ><span style="font-size: 50px; color: #777">-</span>
                <el-input style="width: 40%" size="medium" v-model="form.hlower" placeholder="请填写"
                  ><template slot="append">RH%（下限）</template></el-input
                >
              </el-form-item>
              <el-form-item label="购买日期" prop="buy_date">
                <el-date-picker v-model="form.buy_date" type="date" placeholder="选择日期"> </el-date-picker>
                <!-- <el-input size="medium" v-model="form.buy_date" placeholder="请填写购买日期"></el-input> -->
              </el-form-item>
              <el-form-item label="启用日期" prop="using_date">
                <el-date-picker v-model="form.using_date" type="date" placeholder="选择日期"> </el-date-picker>
                <!-- <el-input size="medium" v-model="form.using_date" placeholder="请填写启用日期"></el-input> -->
              </el-form-item>

              <el-form-item class="submit">
                <el-button
                  v-permission="{ permission: ['创建采集器', '更新采集器'], type: 'disabled' }"
                  type="primary"
                  @click="submitForm('form')"
                  >保 存</el-button
                >
                <el-button @click="resetForm('form')">重 置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Collector from '@/model/Collector'

export default {
  components: {},
  props: {
    isCreate: {
      type: Boolean,
      default: false,
    },
    CollectorId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {},
      data1: [],
      data2: [],
    }
  },
  watch: {},
  async mounted() {
    if (!this.isCreate) {
      const res = await Collector.getDetail(this.CollectorId)
      this.form = res
    }
    const res1 = await Collector.Collector1()
    const res2 = await Collector.Collector2()
    this.data1 = res1
    this.data2 = res2
  },
  // async created() {
  //   this.CollectorData()
  // },
  methods: {
    // CollectorData(){
    //   const res1 = await Collector.Collector1()
    //   const res2 = await Collector.Collector2()
    //   this.data1 =res1
    //   this.data2 =res2
    // },
    // eslint-disable-next-line
    async submitForm(formName) {
      const postData = { ...this.form }
      let res
      if (this.isCreate) {
        res = await Collector.addCollector(postData)
      } else {
        res = await Collector.editCollector(this.CollectorId, postData)
      }
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success(`${res.message}`)
        // this.resetForm(formName)
        this.$confirm('是否返回上一页', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          this.$emit('editClose')
        })
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}

.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;
    display: inline-flex;
    align-content: space-around;
    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .id {
      margin-right: 15px;
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>
